import type { ParentResp } from './parent'
import http from '@/utils/http'

const BASE_URL = '/education/student'

export interface Permission {
  subject: string
  classLevel: number
  admissionYear: number
  classTypeId: string
}

export interface Student {
  id: string
  name: string
  namePinyin: string
  parentId: string
  parentPhone: string
  parentRelation: string
  source?: string
  gender?: number
  birthdate?: string
  avatar?: string
  admissionYear?: string
  enrollStatus?: number
  fulltimeSchool?: string
  remark?: string
  createTime?: string
}

export interface StudentResp extends Student {
  // recommender?: string
}

export interface StudentStatResp {
  /* 总学员数 */
  totalCount: number

  /* 未报名学员总数 */
  noEnrollCount: number

  /* 在线学员总数 */
  onStudyCount: number

  /* 结业学员总数 */
  finishedCount: number

  /* 今日新增数量 */
  todayCount: number
};

export interface StudentCreateReq {
  name: string
  parentId?: string
  parentPhone: string
  parentName?: string
  parentRelation?: string
  source: string
  gender: string
  birthdate?: string
  avatar?: string
  admissionYear?: number
  fulltimeSchool?: string
  remark?: string
}

export interface StudentDetailResp extends StudentResp {
  parent: ParentResp
}

export interface StudentQuery extends SortQuery {
  keyword?: string
  source?: string
  enrollStatus?: number
  admissionYear?: number
  parentPhone?: string
}

export interface StudentPageQuery extends StudentQuery, PageQuery {

}

export interface StudentUpdateReq {
  name: string
  // parentId?: string
  // parentPhone: string
  // parentName: string
  // parentRelation?: string
  source: string
  gender: string
  birthdate?: string
  avatar?: string
  admissionYear?: number
  fulltimeSchool?: string
  remark?: string
}

export interface ParentUpdateReq {
  name: string
  mobilePhone: string
}

// 学生就读班级请求参数
export interface StudentClassQuery {
  id?: string | number
  className?: string
  classStatus?: string | number
}

// 学生就读班级响应数据
export interface StudentClassResp {
  sequence: number
  className: string
  classCategory: string
  term: string
  classTypeName: string
  leadTeacherId: string
  classStatus: string
}

export interface CoursePermisionResp {
  sequence: number
  id: string
  subject: string
  admissionYear: number | string
  classTypeName: string
  classLevel: string | number
  remark: string
  studentId: string
  [propName: string]: any
}

export interface UpdatePermissionQuery {
  studentId?: string
  subject?: string
  classLevel?: number
  admissionYear?: number
  classTypeId?: string
}

export interface StudentShecduleResp {
  lessonDate: string
  startTime: string
  endTime: string
  className: string
  classId: string
  lessonStatus: string
}

/** @desc 查询学员列表 */
export function getStudentPage(query: StudentPageQuery) {
  return http.get<Page<StudentResp[]>>(`${BASE_URL}/page`, query)
}

export function getListByParentId(parentId: string) {
  return http.get<StudentResp[]>(`${BASE_URL}/list-by-parent?parentId=${parentId}`)
}

/** @desc 查询学员详情 */
export function getStudentDetail(id: string) {
  return http.get<StudentDetailResp>(`${BASE_URL}/detail/${id}`)
}

/** @desc 创建学员 */
export function createStudent(data: StudentCreateReq) {
  return http.post(`${BASE_URL}/create`, data)
}

/** @desc 修改学员 */
export function updateStudent(data: StudentUpdateReq, id: string) {
  return http.post(`${BASE_URL}/update-student/${id}`, data)
}

/** @desc 修改家长 */
export function updateParent(data: ParentUpdateReq, id: string) {
  return http.post(`${BASE_URL}/update-parent/${id}`, data)
}

/** @desc 删除学员 */
export function removeStudent(ids: string | Array<string>) {
  return http.post(`${BASE_URL}/delete/${ids}`)
}

// 学生统计
export function getStudentStat() {
  return http.get<StudentStatResp>(`${BASE_URL}/stat`)
}

// 查询就读班级
export function getStudentClass(query: StudentClassQuery & PageQuery) {
  return http.get<Page<StudentClassResp[]>>(`${BASE_URL}/class/page`, query)
}

// 选课权限列表
export function getStudentEnrollPermission(query: PageQuery, id: string | number) {
  return http.get<Page<CoursePermisionResp[]>>(`${BASE_URL}/permission/list/${id}`, query)
}

// 查询家长的孩子个数
export function getParentChild(query: ParentChildQuery) {
  return http.get(`${BASE_URL}/manage/queryOtherStudent`, query)
}

// 班级分类
export function getClassType() {
  return http.get('/education/class-type/list?status=1')
}

export function updateClassPermission(data: UpdatePermissionQuery, id: string | number) {
  return http.post(`${BASE_URL}/permission/update/${id}`, data)
}

export function deleteClassPermission(id: string | number) {
  return http.post(`${BASE_URL}/permission/delete/${id}`)
}

export function addClassPermission(data: UpdatePermissionQuery) {
  return http.post(`${BASE_URL}/permission/create`, data)
}

export function getStudentShecdule(id: string | number) {
  return http.get<StudentShecduleResp[]>(`${BASE_URL}/schedule/${id}`)
}
